<?php 
    require_once ("/mnt/sdcard/www/Includes/simplecms-config.php"); 
    require_once  ("/mnt/sdcard/www/Includes/connectDB.php");
    include("/mnt/sdcard/www/Includes/header.php");

    createLog("manage");

    define(LEND_POSSIBLE, 0);
    define(LEND_IMPOSSIBLE, 1);
    define(LEND_DEVTEAM, 2);
    define(LEND_ING_APPROVAL, 3);
    define(LEND_REPAIR ,9);

    define(OWNER_QATEAM ,0);
    define(OWNER_LEND_DEVICE_SHORT ,1);
    define(OWNER_LEND_DEVICE_LONG ,2);
    define(OWNER_DEVTEAM ,3);

    if(isset($_POST['modify']))
    {
        $list = $_POST['checkbox'];
        $id = $_POST['id'];
        $deviceOS = $_POST['deviceOS'];
        $deviceName = $_POST['deviceName'];
        $deviceModel = $_POST['deviceModel'];
        $deviceVersion = $_POST['deviceVersion'];
        $deviceResolution = $_POST['deviceResolution'];
        $team = $_POST['team'];
        $name = $_POST['name'];
        $startDate = $_POST['startDate'];
        $endDate = $_POST['endDate'];
        $returnDate = $_POST['returnDate'];
        $carrier = $_POST['carrier'];
        $deviceNumber = $_POST['deviceNumber'];
        $etc = $_POST['etc'];
        $deviceID = $_POST['deviceID'];
        $deviceState = $_POST['deviceState'];
        $deviceOwner = $_POST['deviceOwner'];
        $deleted = $_POST['deleted'];

        $creationWasSuccessful = false;
        //$fail = "";
        //$queryList = "";

        for($i=0; $i<count($list); $i++)
        {
            $query = "UPDATE deviceInfo set deviceOS='$deviceOS[$i]', deviceName='$deviceName[$i]', deviceModel='$deviceModel[$i]', deviceVersion='$deviceVersion[$i]', deviceResolution='$deviceResolution[$i]', team='$team[$i]', name='$name[$i]', startDate='$startDate[$i]', endDate='$endDate[$i]', returnDate='$returnDate[$i]', carrier='$carrier[$i]', deviceNumber='$deviceNumber[$i]', etc='$etc[$i]', deviceID='$deviceID[$i]', deviceState='$deviceState[$i]', deviceOwner='$deviceOwner[$i]', deleted='$deleted[$i]' WHERE id='$list[$i]'";
            $statement = $databaseConnection->prepare($query);
            $statement->execute();
            $statement->store_result();

            //$queryList .= $query . "<br>";

            /*if(!$statement->affected_rows == 1 ? true : false)
            {
                $fail .= "$list[$i]/";
                $creationWasSuccessful++;
            }*/

            $creationWasSuccessful = $creationWasSuccessful || ($statement->affected_rows == 1 ? true : false);
        }

        if ($creationWasSuccessful)
        {
            echo $queryList;
            $statement->close();
            echo("
                <script>
                window.alert('변경되었습니다.')
                
                </script>
            ");
        }
        else
        {
            //echo $queryList;
            //$fail = "ID: " . $fail . "(실패: $creationWasSuccessful 개)";
            $statement->close();
            echo("
                <script>
                window.alert('변경실패한 항목이 있습니다.')

                </script>
            ");
        }
    }
    
    $query = "SELECT * FROM deviceInfo";
    $statement = $databaseConnection->prepare($query);
    $statement->execute();
    $statement->store_result();
    $total_record = $statement->affected_rows;   
 ?>

<div class="navbar navbar-inverse navbar-fixed-top">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            </button>
            <?php
                if (logged_on())
                {
                    $url = "/phoneList.php";
                }
                else
                {
                    $url = "/index.php";
                }
            ?>
            <a class="navbar-brand" href="<?php echo $url ?>">Device Management System</a>
        </div>
        <div class="navbar-collapse collapse">
            <ul class="nav navbar-nav">
                <li><a href="/index.php">Home</a></li>
                <?php
                    if (logged_on())
                    {
                        echo '
                            <li><a href="/phoneList.php">기기목록</a></li>
                            <li><a href="/approval.php">결재</a></li>
                        ';
                    }
                ?>
                <?php
                    if (is_admin())
                    {
                        echo '
                            <li class="dropdown" class="active">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown">관리메뉴<b class="caret"></b></a>
                                <ul class="dropdown-menu">
                                    <li><a href="/phoneEnroll.php">기기등록</a></li>
                                    <li><a href="/manage.php">기기관리</a></li>
                                    <!--<li class="divider"></li>
                                    <li class="dropdown-header">Nav header</li>
                                    <li><a href="#">Separated link</a></li>
                                    <li><a href="#">One more separated link</a></li>-->
                                </ul>
                            </li>
                        ';
                    }
                ?>
            </ul>
            <ul class="nav navbar-nav navbar-right">
                <?php
                    if (logged_on())
                    {
                        if (is_admin())
                        {
                            echo "
                                <li><a href='#'>환영합니다, {$_SESSION['username']}(관리자)</a></li>
                                <li><a href='/logoff.php'>로그아웃</a></li>
                            ";
                        }
                        elseif (is_member())
                        {
                            echo "
                                <li><a href='#'>환영합니다, {$_SESSION['username']}(정회원)</a></li>
                                <li><a href='/logoff.php'>로그아웃</a></li>
                            ";   
                        }
                        else
                        {
                            echo "
                                <li><a href='#'>환영합니다, {$_SESSION['username']}(준회원)</a></li>
                                <li><a href='/logoff.php'>로그아웃</a></li>
                            ";
                        }
                    }
                ?>
            </ul>
        </div><!--/.navbar-collapse -->
    </div>
</div>

<div class="container">
    <?php
        if(!is_admin())
        {
            echo("
                <script>
                window.alert('페이지에 접근할 수 있는 권한이 없습니다.')
                location.replace('index.php')
                </script>
                ");
            exit;
        }
    ?>

    <?php if($total_record == 0) :?>
    
    등록된 기기가 없습니다.

    <?php else :?>

    <?php $statement->bind_result($id, $deviceOS, $deviceName, $deviceModel, $deviceVersion, $deviceResolution, $team, $name, $startDate, $endDate, $returnDate, $carrier, $deviceNumber, $etc, $deviceID, $deviceState, $deviceOwner, $deleted); ?>
    <p><form name="manage" action="manage.php" method="post"><button type="submit" name="modify" class="btn btn-primary">변경하기</button>
    &nbsp;&nbsp;전체기기: <?php echo $total_record ?>대</p>
    <div class="table-responsive">
    <div class="panel panel-primary">
        <div class="panel-heading">대여기기 목록</div>
        <table class="table table-hover table-striped">
            <thead><tr>
                <th class="text-center">ID</th>
                <th class="text-center">운영체제</th>
                <th class="text-center">기기명</th>
                <th class="text-center">모델명</th>
                <th class="text-center">버전</th>
                <th class="text-center">해상도</th>
                <th class="text-center">팀</th>
                <th class="text-center">이름</th>
                <th class="text-center">대여시작일</th>
                <th class="text-center">대여종료일</th>
                <th class="text-center">기기반납일</th>
                <th class="text-center">통신사</th>
                <th class="text-center">번호</th>
                <th class="text-center">비고</th>
                <th class="text-center">기기ID</th>
                <th class="text-center">기기상태</th>
                <th class="text-center">소유자</th>
                <th class="text-center">삭제</th>
            </tr></thead>

        <?php 
            $pickerID = 0;
            while($statement->fetch()) :
        ?>

            <?php
                switch($deleted)
                {
                    case 0:
                        $tag = "class=\"text-center\"";
                        break;
                    case 1:
                        $tag = "class=\"text-center danger\"";
                        break;
                }

                if($startDate == "0000-00-00")
                {
                    $startDate = "";
                }

                if($endDate == "0000-00-00")
                {
                    $endDate = "";
                }

                if($returnDate == "0000-00-00")
                {
                    $returnDate = "";
                }
            ?>

            <tr <?php echo $tag ?>>
                <td><input type="hidden" name="checkbox[]" id="checkbox" value="<?php echo $id ?>"><?php echo $id ?></td>
                <td>
                    <select class="form-control input-sm" name="deviceOS[]">
                        <option value="Android" <? if($deviceOS == 'Android') { echo "selected"; } ?>>Android</option>
                        <option value="iOS" <? if($deviceOS == 'iOS') { echo "selected"; } ?>>iOS</option>
                        <option value="etc" <? if($deviceOS == 'etc') { echo "selected"; } ?>>기타</option>
                    </select>
                </td>
                <td><input type="text" class="form-control input-sm" name="deviceName[]" size="10" value="<?php echo $deviceName ?>"></td>
                <td><input type="text" class="form-control input-sm" name="deviceModel[]" size="10" value="<?php echo $deviceModel ?>"></td>
                <td><input type="text" class="form-control input-sm" name="deviceVersion[]" size="3" value="<?php echo $deviceVersion ?>"></td>
                <td><input type="text" class="form-control input-sm" name="deviceResolution[]" size="9" value="<?php echo $deviceResolution ?>"></td>
                <td><input type="text" class="form-control input-sm" name="team[]" size="5" value="<?php echo $team ?>"></td>
                <td><input type="text" class="form-control input-sm" name="name[]" size="5" value="<?php echo $name ?>"></td>
                <td><input type="text" class="form-control input-sm" name="startDate[]" size="10" value="<?php echo $startDate ?>" id="dp1"></td>
                <td><input type="text" class="form-control input-sm" name="endDate[]" size="10" value="<?php echo $endDate ?>" id="dp1-1"></td>
                <td><input type="text" class="form-control input-sm" name="returnDate[]" size="10" value="<?php echo $returnDate ?>" id="dp1-2"></td>
                <td>
                    <select class="form-control input-sm" name="carrier[]">
                        <option value="" <? if($carrier == '') { echo "selected"; } ?>>없음</option>
                        <option value="SKT" <? if($carrier == 'SKT') { echo "selected"; } ?>>SKT</option>
                        <option value="KT" <? if($carrier == 'KT') { echo "selected"; } ?>>KT</option>
                        <option value="LGT" <? if($carrier == 'LGT') { echo "selected"; } ?>>LGT</option>
                    </select>
                </td>
                <td><input type="text" class="form-control input-sm" name="deviceNumber[]" size="14" value="<?php echo $deviceNumber ?>"></td>
                <td><input type="text" class="form-control input-sm" name="etc[]" size="10" value="<?php echo $etc ?>"></td>
                <td><input type="text" class="form-control input-sm" name="deviceID[]" size="5" value="<?php echo $deviceID ?>"></td>
                <td>
                    <select class="form-control input-sm" name="deviceState[]">
                        <option value="0" <? if($deviceState == LEND_POSSIBLE) { echo "selected"; } ?>>대여가능</option>
                        <option value="1" <? if($deviceState == LEND_IMPOSSIBLE) { echo "selected"; } ?>>대여중</option>
                        <option value="2" <? if($deviceState == LEND_DEVTEAM) { echo "selected"; } ?>>개발팀대여중</option>
                        <option value="3" <? if($deviceState == LEND_ING_APPROVAL) { echo "selected"; } ?>>결재진행중</option>
                        <option value="9" <? if($deviceState == LEND_REPAIR) { echo "selected"; } ?>>수리중</option>
                    </select>
                </td>
                <td>
                    <select class="form-control input-sm" name="deviceOwner[]">
                        <option value="0" <? if($deviceOwner == OWNER_QATEAM) { echo "selected"; } ?>>QA팀</option>
                        <option value="1" <? if($deviceOwner == OWNER_LEND_DEVICE_SHORT) { echo "selected"; } ?>>대여기기</option>
                        <option value="2" <? if($deviceOwner == OWNER_LEND_DEVICE_LONG) { echo "selected"; } ?>>장기대여기기</option>
                        <option value="3" <? if($deviceOwner == OWNER_DEVTEAM) { echo "selected"; } ?>>개발팀</option>
                    </select>
                </td>
                <td>
                    <select class="form-control input-sm" name="deleted[]">
                        <option value="1" <? if($deleted == 1) { echo "selected"; } ?>>Y</option>
                        <option value="0" <? if($deleted == 0) { echo "selected"; } ?>>N</option>
                    </select>
                </td>
            </tr>
            <?php $pickerID++; ?>
        <?php endwhile?>
        </table>
    </div>
    </div>
    </form>
                           
    <?php endif?>
    <?php $statement->close();?>
</div>

<?php 
    include ("/mnt/sdcard/www/Includes/footer.php");
?>